package cn.stylefeng.guns.modular.rest.xhTransport;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.hx.storage.entity.HxXhTransport;
import cn.stylefeng.guns.modular.hx.storage.model.params.HxXhTransportParam;
import cn.stylefeng.guns.modular.hx.storage.model.result.*;
import cn.stylefeng.guns.modular.rest.aop.UserAop;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.guns.modular.hx.storage.service.HxXhTransportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;


/**
 *地址控制器
 */
@Controller
@RequestMapping("/rest/xhTransportController")
public class restXhTransportController extends BaseController {

    @Autowired
    private UserAop userAop;

    private Lock lock = new ReentrantLock();

    @Autowired
    private HxXhTransportService hxXhTransportService;

    /**
     *提交接口
     *ZDW
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(HxXhTransportParam hxXhTransportParam) {
        User user = userAop.getUser();
        if(user !=null && user.getUserId() != null){
            //创建人
            hxXhTransportParam.setCreateUser(user.getAccount());

            //创建人姓名
            hxXhTransportParam.setCreateUserName(user.getName());
            //创建人部门
            hxXhTransportParam.setDeptId(user.getDeptId());
            //项目id
            hxXhTransportParam.setOperateId(user.getOperateId());
        }
        // 创建时间
        hxXhTransportParam.setCreateTime(new Date());
        //开始位置编号
        hxXhTransportParam.setSrcPositionCode(hxXhTransportParam.getSrcPositionCode());
        //结束位置编号
        hxXhTransportParam.setTarPositionCode(hxXhTransportParam.getTarPositionCode());
        hxXhTransportParam.setState("待接单");
        hxXhTransportService.add(hxXhTransportParam);
        return ResponseData.success("添加任务成功");
    }

    /**
     *查询接口 展示所有state='待接单'的任务
     *wangzhen
     */
    @RequestMapping("/queryItem")
    @ResponseBody
    public LayuiPageInfo queryItem(HxXhTransportParam hxXhTransportParam,String condition) {
        User user = userAop.getUser();
        hxXhTransportParam.setDeptId(user.getDeptId());
       hxXhTransportParam.setOperateId(user.getOperateId());
        // 展示所有state='待接单'的任务
      hxXhTransportParam.setState("待接单");

     return hxXhTransportService.findPageBySpec(hxXhTransportParam,condition);

    }
    /**
     *  展示所有state='已接单'的任务
     *   wangzhen
     */
    @RequestMapping("/queryAllItem")
    @ResponseBody
    public ResponseData queryAllItem(HxXhTransportParam hxXhTransportParam,String condition) {
        User user = userAop.getUser();
        hxXhTransportParam.setDeptId(user.getDeptId());
        hxXhTransportParam.setOperateId(user.getOperateId());
        // 展示所有state='已接单'的任务
        hxXhTransportParam.setState("已接单");
       List<HxXhTransport> list = hxXhTransportService.queryAllItem(hxXhTransportParam,condition);

        return ResponseData.success(list);
    }

    /**
     * 领取任务
     */
    @ResponseBody
    @RequestMapping("/receive")
    public ResponseData receive(HxXhTransportParam  hxXhTransportParam) {
        User user = userAop.getUser();
        lock.lock();//获取锁
        try {
            HxXhTransportParam  hxXhTransportParam1 = new HxXhTransportParam();
            hxXhTransportParam1.setState("已接单");
            hxXhTransportParam1.setTransportUser(user.getAccount());
            HxXhTransportResult hxXhTransportResult = hxXhTransportService.findcheckBySpec(hxXhTransportParam1);
            if (hxXhTransportResult!=null){
                return ResponseData.error("请先完成上一单");
            }
            HxXhTransport hxXhTransport = hxXhTransportService.getById(hxXhTransportParam.getId());
            if (hxXhTransport.getState().equals("待接单")) {
                hxXhTransportParam.setState("已接单");
                hxXhTransportParam.setTransportUser(user.getAccount());
                hxXhTransportParam.setTransportUserName(user.getName());
                hxXhTransportParam.setAcceptOrdersTime(new Date());
                hxXhTransportService.update(hxXhTransportParam);
            } else {
                return ResponseData.error("任务已无法领取");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();//释放锁
        }
        return ResponseData.success();
    }

    /**
     * 提交任务
     */
    @ResponseBody
    @RequestMapping("/reviewCompleted")
    public ResponseData reviewCompleted(HxXhTransportParam hxXhTransportParam) {
        lock.lock();//获取锁
        try {
            HxXhTransportResult res = hxXhTransportService.findCountGoodsBySpec(hxXhTransportParam);
            if(res == null || !res.getState().equals("已接单")) {
                return ResponseData.error("搬运任务不存在或已完成");
            }
                hxXhTransportParam.setId(res.getId());
                hxXhTransportParam.setState("已完成");
                hxXhTransportParam.setCompleteTime(new Date());
                hxXhTransportService.update(hxXhTransportParam);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();//释放锁
        }
        return ResponseData.success();
    }
}
